﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DarkLordsApprenticeV2
{
    class Apprentice : DrawableGameComponent
    {
        public Apprentice(Game g)
            : base(g)
        {
            Position = new Vector2(650, 500);
            state = State.Idle;
            index = 0;
            animCounter = 0;
            frames = new Texture2D[2][];
            frames[0] = new Texture2D[1];
            frames[1] = new Texture2D[3];
            evilnessLevel = 1;
            maxMagic = 50;
            currentMagic = 50;
        }

        // class data
        public Vector2 Position { get; set; }
        enum State { Idle, Casting };
        int index;
        int animCounter = 0;
        State state;
        Texture2D[][] frames;
        Texture2D image;
        SpriteBatch spriteBatch;
        
        // Attributes
        int exp;
        int evilnessLevel;
        int maxMagic;
        int currentMagic;

        protected override void LoadContent()
        {
            frames[0][0] = Game.Content.Load<Texture2D>("Apprentice");
            frames[1][0] = Game.Content.Load<Texture2D>("ApprenticeCasting1");
            frames[1][1] = Game.Content.Load<Texture2D>("ApprenticeCasting2");
            frames[1][2] = Game.Content.Load<Texture2D>("ApprenticeCasting3");

            //frame = Game.Content.Load<Texture2D>("IntroScreen");

            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            image = frames[0][0];
        }

        public override void Update(GameTime gameTime)
        {
            switch (exp)
            {
                case 10:
                    maxMagic = 100;
                    evilnessLevel = 2;
                    break;

                case 30:
                    maxMagic = 150;
                    evilnessLevel = 3;
                    break;

                case 60:
                    maxMagic = 200;
                    evilnessLevel = 4;
                    break;

                case 100:
                    maxMagic = 250;
                    evilnessLevel = 5;
                    break;

                case 150:
                    maxMagic = 300;
                    evilnessLevel = 6;
                    break;

                case 210:
                    maxMagic = 350;
                    evilnessLevel = 7;
                    break;

                case 280:
                    maxMagic = 400;
                    evilnessLevel = 8;
                    break;

                case 360:
                    maxMagic = 500;
                    evilnessLevel = 9;
                    break;
            }

            int stateIndex = 0;
            switch (state)
            {
                case State.Idle:
                    stateIndex = 0;
                    break;

                case State.Casting:
                    stateIndex = 1;
                    break;
            }

            if (animCounter % 20 == 0)
            {
                index++;
                if (index >= frames[stateIndex].Length)
                {
                    index = 0;
                }
                image = frames[stateIndex][index];
            }

            animCounter++;

            currentMagic++;
            if (currentMagic > maxMagic)
            {
                currentMagic = maxMagic;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(image, Position, Color.White);
            spriteBatch.End();
        }

        public void SwitchToIdle()
        {
            state = State.Idle;
            index = 0;
            animCounter = 0;
        }

        public void SwitchToCasting()
        {
            state = State.Casting;
            index = 0;
            animCounter = 0;
        }

        public int GetEvilnessLevel()
        {
            return evilnessLevel;
        }

        public int GetCurrentMagic()
        {
            return currentMagic;
        }

        public int GetMaxMagic()
        {
            return maxMagic;
        }

        public void GainEXP(int moreEXP)
        {
            exp += moreEXP;
        }

        public void SpendMagic()
        {
            currentMagic -= 2;
            if (currentMagic < 0)
            {
                currentMagic = 0;
            }
        }

    }
}